package com.miracle.java2110.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.miracle.java2110.common.PageVo;
import com.miracle.java2110.common.R;

import com.miracle.java2110.listener.WebServicedListener;
import com.miracle.java2110.pojo.ServiceType;
import com.miracle.java2110.pojo.Serviced;
import com.miracle.java2110.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/service")
@CrossOrigin("*")
public class ServiceController {
    @Autowired
    private MyService myService;

    @Autowired
    private WebServicedListener webServicedListener;

    @RequestMapping("/pageData")
    public PageVo<ServiceType> pageData(@RequestParam Map<String, Object> param) {
        PageVo<ServiceType> serviceTypePageVo = myService.selectAll(param);

        return serviceTypePageVo;
    }

    @RequestMapping("/pageDataService")
    public PageVo<Serviced> pageDataService(@RequestParam Map<String, Object> param) {
        PageVo<Serviced> servicedPageVo = myService.selectAllService(param);

        return servicedPageVo;
    }

    @RequestMapping("/add")
    public R addServiceType(ServiceType st) {
        return myService.addServiceType(st);
    }

    @RequestMapping("/delete")
    public R deleteById(Integer id) {
        return myService.deleteById(id);
    }

    @RequestMapping("/update")
    public R updateById(ServiceType st) {
        return myService.updateById(st);
    }

    @RequestMapping("/addService")
    public R addService(Serviced s) {
        return myService.addService(s);
    }

    @RequestMapping("/deleteService")
    public R deleteServiceById(Integer id) {
        return myService.deleteServiceById(id);
    }

    @RequestMapping("/updateService")
    public R updateService(Serviced serviced) {
        return myService.updateService(serviced);
    }

    @RequestMapping("/dels")
    public R deleteServices(int[] array) {
//        int i = 1;
//        if (i == 1) {
//            throw new CustomerException("该用户没有权限");
//        }
        return myService.deleteServiced(array);
    }

    @RequestMapping("/upload")
    public R readExcel(@RequestParam MultipartFile file) throws IOException {
        ExcelReaderBuilder builder = EasyExcel.read(file.getInputStream(), Serviced.class, webServicedListener);

        ExcelReaderSheetBuilder sheet = builder.sheet();

        sheet.doRead();

        return R.ok(200, "导入成功");
    }
}
